package zhou.jan21;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Login() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		
		String name=request.getParameter("username");
		String pwd=request.getParameter("pwd");
		System.out.println("name="+name+"   pwd="+pwd);
		UserService us=new UserService();
		boolean result=false;        //默认false，有异常也是false
		try {
			result=us.login(name, pwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();       //返回false
		}
		if(result){
			response.sendRedirect("zhouLoginSuccess.html");
		}
		else{
			response.sendRedirect("zhouLoginFail.html");
		}
		
		// 重定向response("页面的地址")：地址会变化，变成zhouLoginSuccess.html
		//response.sendRedirect("zhouLoginSuccess.html");
		//请求转发：地址不会变化
		//request.getRequestDispatcher("zhouLoginSuccess.html").forward(request, response);;
				
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
